//
//  USCollectionMenu.m
//  usale0001
//
//  Created by YU on 16/1/22.
//  Copyright © 2016年 YU. All rights reserved.
//

#import "USCollectionMenu.h"


@interface USCollectionMenu()
@property (nonatomic, assign) CGPoint origin;
@end


@implementation USCollectionMenu

- (instancetype)initWithOrigin:(CGPoint)origin titleArray:(NSArray *)titleArray colorArray:(NSArray *)colorArray{
    self = [super initWithFrame:CGRectMake(0, 0, kUSScreenWidth, kUSScreenHeight)];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
        self.origin = origin;
        self.titleArray = [titleArray copy];
        self.colorArray = [colorArray copy];
        
        
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        layout.minimumLineSpacing = 0;
        layout.minimumInteritemSpacing = 0;
        layout.itemSize = CGSizeMake(kUSScreenWidth / 4, 36);
        
        CGFloat height = ((self.titleArray.count - 1) / 4  + 1)* 36;
        self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(origin.x, origin.y, kUSScreenWidth, height) collectionViewLayout:layout];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        [_collectionView setBackgroundColor:[UIColor whiteColor]];
        [_collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"USCollectionMenu"];
        
        UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(origin.x, origin.y + height - 1, kUSScreenWidth, 1)];
        lineView.backgroundColor = kUSColorSeparatorColor;
        
        
        
        [self addSubview:_collectionView];
        [self addSubview:lineView];
    }
    return self;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.titleArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"USCollectionMenu" forIndexPath:indexPath];
    
    CGFloat cellWidth = kUSScreenWidth / 4;
    
    UIView *colorView = [[UIImageView alloc] initWithFrame:CGRectMake(5 , 11, 14, 14)];
    [colorView setBackgroundColor:self.colorArray[indexPath.row]];
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(24, 0, cellWidth - 20, 36)];
    label.text = self.titleArray[indexPath.row];
    label.textAlignment = NSTextAlignmentLeft;
    [label setFont:[UIFont systemFontOfSize:14]];
    [label setTintColor:kUSColorFromRGB(0x333333)];
    
    [cell.contentView addSubview:colorView];
    [cell.contentView addSubview:label];
    
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    if ([self.delegate respondsToSelector:@selector(usCollectionMenu:didSelectItemAtIndexPath:)]) {
        [self.delegate usCollectionMenu:collectionView didSelectItemAtIndexPath:indexPath];
    }
    [collectionView deselectItemAtIndexPath:indexPath animated:YES];
    if (!self.NotDissmiss) {
        [self dismissWithCompletion:nil];
        
    }else{
        
        if (self.dismiss) {
            
            self.dismiss();
        }
    }
}
- (void)dismissWithCompletion:(void (^)(USCollectionMenu *))completion{
    __weak __typeof(self) weakSelf = self;
    [UIView animateWithDuration:0.1 animations:^{
        weakSelf.alpha = 0;
        weakSelf.collectionView.frame = CGRectMake(weakSelf.origin.x, weakSelf.origin.y, 0, 0);
       
    } completion:^(BOOL finished) {
        [weakSelf removeFromSuperview];
        if (completion) {
            completion(weakSelf);
        }
        if (weakSelf.dismiss) {
            weakSelf.dismiss();
        }
    }];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];
    if (![touch.view isEqual:self.collectionView]) {
        [self dismissWithCompletion:nil];
    }
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
}


@end
